#include "mainwindow.h"
#include "ui_mainwindow.h"

bool is_modified = false;
tileset_mem_t* memtileset=NULL;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->mainToolBar->setMaximumHeight(32);
    //ui->mainToolBar->setMaximumHeight(30);
    is_modified = false;
    QDir work_root_path;
    work_path = work_root_path.absolutePath();
    ui->scrollArea_2->setMinimumSize(0,0);


    map = NULL;
    map = new Wt_Map();
    ui->scrollArea->setMinimumSize(0,0);
    ui->scrollArea->setWidget(map);


    wt_tileset = NULL;


}

MainWindow::~MainWindow()
{
    delete ui;
    delete map;
    if(NULL!=wt_tileset)delete wt_tileset;
}


bool MainWindow::create_new_tileset()
{
    if(NULL!=wt_tileset&&NULL!=wt_tileset->tileset)
    {
        COM_MSBOX_WARNING("tileset is created <b>already</b> !");
        return false;
    }

    bool isOK;
    tileset_name = QInputDialog::getText(this, "New TileSet", "Please name the tileset", QLineEdit::Normal, "new tileset", &isOK);
    if(!isOK)
    {
        return false;
    }
    if(tileset_name.isEmpty())
    {
        COM_MSBOX_WARNING("tileset's name cannot be empty !");
        return false;
    }
    else
    {
        tileset_name.append(".tsx"); // add tileset type
    }

    if(!QDir::setCurrent(work_path)|| !QDir::setCurrent("tilesets"))
    {
        COM_MSBOX_WARNING("failed to switch to dir tilesets !");
        return false;
    }
     if(com_check_file_exist(tileset_name))
    {
         COM_MSBOX_WARNING("tileset is already exist !");
         return false;
    }

     //printf("new tileset name is %s\n", tileset_name.toAscii().data());
     wt_tileset = new Wt_TileSet();
     memtileset = wt_tileset->tileset;
     ui->scrollArea_2->setWidget(wt_tileset);
    // tileset_mem_t* memtileset = wt_tileset->tileset;

     return true;
 }

bool MainWindow::load_exist_tileset()
{
    //switch to dir tilesets
    QDir::setCurrent(work_path);
    QDir::setCurrent("tilesets");
    QString fn = QFileDialog::getOpenFileName(this, tr("Open Tile..."),QString(),tr("TILESET-Files (*.tsx);;All Files (*)"));
    if(fn.isEmpty())
    {
		//it should pass if user cancel the load action
        //COM_MSBOX_CRITICAL("tileset's name cannot be empty!");
        return false;
    }
    if(!com_check_file_exist(fn))
    {
        COM_MSBOX_CRITICAL("tileset not exist !!");
        return false;
    }
    tileset_name = fn;

    return true;
}

bool MainWindow::close_current_tileset()
{
    if(NULL!=wt_tileset)delete wt_tileset;
    wt_tileset =NULL;
    is_modified = false;
    return true;
}

void MainWindow::on_action_New_TileSet_triggered()
{
    this->create_new_tileset();
}

void MainWindow::on_action_Save_TileSet_triggered()
{
   if(NULL!=wt_tileset)
   {
        if(QDir::setCurrent(work_path)&& QDir::setCurrent("tilesets"))
        {
           if(!tileset_name.isEmpty())
            {
                wt_tileset->saveFile(tileset_name);
            }
        }
        else
        {
            COM_MSBOX_WARNING("failed to switch to dir tilesets !");
        }
   }
   else
   {
       COM_MSBOX_WARNING("tileset is not created!");
   }
}

void MainWindow::on_action_Load_TileSet_triggered()
{
    if(NULL!=wt_tileset)
    {
        COM_MSBOX_WARNING("A tileset is created, please close it before loading another one!");
    }
    else
    {
       if(true==this->load_exist_tileset())
	   {
             wt_tileset = new Wt_TileSet();
             ui->scrollArea_2->setWidget(wt_tileset);
             memtileset = wt_tileset->tileset;
         wt_tileset->loadFile(tileset_name);
	   }
    }

}

void MainWindow::on_action_Close_TileSet_triggered()
{
    //this->close_current_tileset();

    if(NULL!=wt_tileset)
    {
        if(true==is_modified)
        {
         QMessageBox msgBox;
         msgBox.setParent(this,Qt::Dialog);
         msgBox.setText("The tileset is not saved.");
         msgBox.setInformativeText("Do you want to save it?");
         msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Cancel);
         msgBox.setDefaultButton(QMessageBox::Save);

         int ret = msgBox.exec();

           switch (ret)
           {
              case QMessageBox::Save:
               // Save was clicked
               this->on_action_Save_TileSet_triggered();
               this->close_current_tileset();
               break;
              case QMessageBox::Cancel:
               // Cancel was clicked
               this->close_current_tileset();
               break;
              default:
               // should never be reached
               break;
           }
        }
        else
        {
            this->close_current_tileset();
        }
     }
    else
    {
          COM_MSBOX_WARNING("tileset is not created!");
    }


}
void MainWindow::closeEvent(QCloseEvent *event)
{
        if(false==is_modified)
        {
            this->close();
        }
        else
        {
            this->on_action_Close_TileSet_triggered();
            this->close();
        }
}

void MainWindow::on_action_Insert_Tile_triggered()
{
    if(NULL!=wt_tileset)
    {
        if(QDir::setCurrent(work_path)&&QDir::setCurrent("tiles"))
        {
            wt_tileset->addTile();
        }
        else
        {
            COM_MSBOX_WARNING("failed to switch to dir tiles !");
        }
    }
    else
    {
        COM_MSBOX_WARNING("tileset is not created!");
    }
}

void MainWindow::on_action_Delete_Tile_triggered()
{
   if(NULL!=wt_tileset)
   {
       wt_tileset->delTile();
   }
   else
   {
       COM_MSBOX_WARNING("tileset is not created!");
   }
}
